package se2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class Administrator
{
    private int     id;
    private String  name;
    private String  email;
    private String  username;
    private String  password;
    private boolean success;
    private String  msg = "";

    public Administrator(int id)
    {
	// super();
	this.id = id;

	try
	{
	    Connection conn = getConnection();
	    PreparedStatement ps;
	    ResultSet rs;

	    ps = conn.prepareStatement("select * from AdminInfo where id = ? ");
	    ps.setInt(1, this.id);
	    rs = ps.executeQuery();

	    if (rs.next())
	    {
		this.id = rs.getInt(1);
		this.name = rs.getString(2);
		this.email = rs.getString(3);
		this.username = rs.getString(4);
		this.password = rs.getString(5);
	    }
	}
	catch (Exception e)
	{
	    System.out.println("Exception HIT in Administrator - constructor");
	    e.printStackTrace();
	}

    }

    public Administrator()
    {
    }

    public boolean isSuccess()
    {
	return success;
    }

    public void setSuccess(boolean success)
    {
	this.success = success;
    }

    public String getName()
    {
	return name;
    }

    public void setName(String name)
    {
	this.name = name;
    }

    public String getEmail()
    {
	return email;
    }

    public void setEmail(String email)
    {
	this.email = email;
    }

    public String getUsername()
    {
	return username;
    }

    public void setUsername(String username)
    {
	this.username = username;
    }

    public String getPassword()
    {
	return password;
    }

    public void setPassword(String password)
    {
	this.password = password;
    }

    public int getId()
    {
	return id;
    }

    public void setId(int id)
    {
	this.id = id;
    }

    public String getMsg()
    {
	return msg;
    }

    public void setMsg(String msg)
    {
	this.msg = msg;
    }

    private Connection getConnection() throws ClassNotFoundException, SQLException
    {
	Class.forName("org.sqlite.JDBC");
	String home = System.getProperty("user.home");
	Connection conn = DriverManager.getConnection("jdbc:sqlite:" + home + "/db/VoIP");

	return conn;
    }

    public List<Administrator> getAllAdminAccts()
    {

	try
	{
	    Connection conn = getConnection();
	    PreparedStatement ps;
	    ResultSet rs;

	    ps = conn.prepareStatement("select id from AdminInfo");
	    rs = ps.executeQuery();

	    List<Administrator> admins = new ArrayList<Administrator>();

	    while (rs.next())
	    {
		Administrator a = new Administrator(rs.getInt(1));
		admins.add(a);
	    }

	    rs.close();
	    conn.close();

	    return admins;
	}
	catch (Exception e)
	{
	    System.out.println("Exception HIT in Adminstrator.java - getAllAdministrators()");
	    return null;
	}

    }

    public boolean addAdministrator()
    {

	try
	{
	    Connection conn = getConnection();
	    PreparedStatement ps;
	    ResultSet rs;

	    ps = conn.prepareStatement("select username from AdminInfo where username = ? ");
	    ps.setString(1, this.username);
	    rs = ps.executeQuery();

	    if (rs.next())
	    {
		rs.close();
		conn.close();
		this.msg = "Username%20is%20already%20in%20use";
		return false;
	    }
	    else
	    {
		ps = conn
			.prepareStatement("insert into admininfo (name, email, username, password) values ( ? , ? , ? , ? )");
		ps.setString(1, this.name);
		ps.setString(2, this.email);
		ps.setString(3, this.username);
		ps.setString(4, this.password);
		ps.executeUpdate();
		rs.close();

		conn.close();
		
		return true;
	    }

	}
	catch (Exception e)
	{
	    System.out.println("ValidateUsername Exception HIT");
	    return false;
	}
    }
    
    public void loadAllInformation()
    {
	try
	{
	    Connection conn = getConnection();
	    
	    PreparedStatement ps;
	    ResultSet rs;

	    ps = conn.prepareStatement("select * from AdminInfo where id = ?");
	    ps.setInt(1, this.id);
	    rs = ps.executeQuery();
	    
	    if (rs.next())
	    {
		this.id = rs.getInt(1);
		this.name = rs.getString(2);
		this.email = rs.getString(3);
		this.username = rs.getString(4);
		this.password = rs.getString(5);
	    }
	    rs.close();
	    
	    conn.close();
	}
	catch (Exception e)
	{
	    System.out.println("Failed to edit the Administrator.");
	}
    }

    public boolean editAdministrator()
    {
	try
	{
	    Connection conn = getConnection();
	    
	    PreparedStatement ps;

	    System.out.println ("EDIT: " + this.id);
	    
	    ps = conn.prepareStatement("update AdminInfo set name=?, email=?, username=?, password=? where id=?");
	    ps.setString(1, this.name);
	    ps.setString(2, this.email);
	    ps.setString(3, this.username);
	    ps.setString(4, this.password);
	    ps.setInt(5, this.id);
	    ps.execute();
	    
	    conn.close();
	    
	    return true;
	}
	catch (Exception e)
	{
	    System.out.println("Failed to edit the Administrator.");
	    return false;
	}
    }
    
    public boolean removeAdministrator()
    {
	try
	{
	    Connection conn = getConnection();
	    
	    PreparedStatement ps;
	    
	    ps = conn.prepareStatement("delete from AdminInfo where id=?");
	    ps.setInt(1, this.id);
	    ps.execute();
	    
	    conn.close();
	    
	    return true;
	}
	catch (Exception e)
	{
	    System.out.println("Failed to remove the Administrator.");
	    return false;
	}
    }
    
}
